//
//  Akeeta_NetWorking.m
//  yaguan
//
//  Created by Feng on 2019/4/15.
//  Copyright © 2019年 yaguan. All rights reserved.
//

#import "Akeeta_NetWorking.h"
#import <AFNetworking/AFNetworking.h>

#define TIMEOUTINTERVALFORREQUEST  (30.0)//默认超时长

static AFHTTPSessionManager *manager = nil;

@interface Akeeta_HttpNetWorking : NSObject

@end

@implementation Akeeta_HttpNetWorking

+ (AFHTTPSessionManager *)sharedManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        config.timeoutIntervalForRequest = 30;
        manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain",@"text/javascript",@"text/json", nil];
    });
    return manager;
}

@end

@implementation Akeeta_NetWorking


+ (NSURLSessionDataTask *)postWithPath:(NSString *_Nullable)url
                                params:(NSDictionary *_Nullable)params
                           clientToken:(NSString *_Nullable)token
                               success:(HttpSuccessBlock)success
                               failure:(HttpFailureBlock)failure {

    __weak AFHTTPSessionManager *manager = [Akeeta_HttpNetWorking sharedManager];
    
    //添加请求头部
    if (token) {
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
    }
    
    NSURLSessionDataTask *task = [manager POST:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];

    return task;
}

+ (NSURLSessionDataTask *)postWithPath:(NSString *_Nullable)url
                                params:(NSDictionary *_Nullable)params
                               success:(HttpSuccessBlock)success
                               failure:(HttpFailureBlock)failure {
    
    __block  AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",nil];
    
    NSURLSessionDataTask *dataTask =  [manager POST:url parameters:params headers:@{} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
    return dataTask;
}


+ (NSURLSessionDataTask *)getWithPath:(NSString *_Nullable)url
     params:(NSDictionary * _Nullable)params
clientToken:(NSString *_Nullable)token
    success:(HttpSuccessBlock)success
    failure:(HttpFailureBlock)failure{
    
    __weak AFHTTPSessionManager *manager = [Akeeta_HttpNetWorking sharedManager];
    
    //添加请求头部
    if (token) {
        [manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
    }
    
    NSURLSessionDataTask *task = [manager GET:url parameters:params headers:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)  {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];

    return task;
}

@end
